'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: uploader_handler.py
* @Time: 2025/9/25
* @All Rights Reserve By Brtc
'''
from dataclasses import dataclass

from flask_login import login_required, current_user
from injector import inject

from internal.schema.upload_file_schema import UploadReq, UploadFileResp, UploadImageReq
from internal.service import CosService
from pkg.response import validate_error_json, success_json


@inject
@dataclass
class UploaderHandler:
    """上传文件 http 业务请求"""
    cos_service:CosService

    @login_required
    def upload_file(self):
        """上传文档http 处理器"""
        req = UploadReq()
        if not req.validate():
            return validate_error_json(req.errors)
        #2、调用服务上传文件并获取记录
        upload_file = self.cos_service.upload_file(req.file.data, current_user)
        #3、构建响应并返回
        resp = UploadFileResp()
        return success_json(resp.dump(upload_file))


    @login_required
    def upload_image(self):
        """上传图片 http请求处理"""
        #1、构建请求并校验
        req = UploadImageReq()
        if not req.validate():
            return validate_error_json(req.errors)
        #2、调用服务并上传
        upload_file = self.cos_service.upload_file(req.file.data, current_user,True)
        # 3、返回图片的地址
        image_url = self.cos_service.get_file_url(upload_file.key)
        return success_json({"image_url":image_url})


